﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Toolbox
{
    public static class GeneralTools
    {
        #region Reflection
        public static object InvokeMethod(FileInfo assemblyPath, string methodName, object[] methodParameters, string subject)
        {
            FileInfo fileInfo = assemblyPath;

            Assembly assembly = Assembly.LoadFile(fileInfo.FullName);
            Type type = assembly.GetType(GetClassBySubject(subject));

            object result = null;
            
            if (type != null)
            {
                MethodInfo methodInfo = type.GetMethod(methodName);

                if (methodInfo != null)
                {

                    ParameterInfo[] parameters = methodInfo.GetParameters();
                    object classInstance = Activator.CreateInstance(type, null);

                    if (parameters.Length == 0)
                    {
                        result = methodInfo.Invoke(classInstance, null);
                    }
                    else
                    {
                        object[] parametersArray = methodParameters;

                        result = methodInfo.Invoke(classInstance, parametersArray);
                    }
                }
            }

            return result;
        }

        private static string GetClassBySubject(string subject)
        {
            string result = "";

            switch (subject)
            {
                case "Activity":
                    result = "FremtidensPlejehjem.Controllers.CtrlActivity";
                    break;
                case "Restaurant":
                    result = "FremtidensPlejehjem.Controllers.CtrlRestaurant";
                    break;
                case "Room":
                    result = "FremtidensPlejehjem.Controllers.CtrlRoom";
                    break;
                default:
                    break;
            }

            return result;
        }
        #endregion

        #region Messaging
        public static T DeserializeMessage<T>(Message message)
        {
            T result;

            XmlSerializer xsl = new XmlSerializer(typeof(T));

            StringReader sReader = new StringReader(message.Body.ToString());

            result = (T)xsl.Deserialize(sReader);

            return result;
        }

        public static string SerializeMessage(object objectToBeSerialized)
        {
            string result = "";

            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(objectToBeSerialized.GetType());
            StringWriter sw = new StringWriter();
            XmlWriter xw = XmlWriter.Create(sw);

            x.Serialize(xw, objectToBeSerialized);

            result = sw.ToString();

            return result;
        }
        #endregion
    }
}
